package com.lzt.admin.system.controller;

import com.lzt.admin.common.captcha.config.CaptchaCore;
import com.lzt.admin.common.result.ResultJson;
import com.lzt.admin.system.service.SysConfigService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@RequestMapping("sys")
public class CaptchaController {
    private final SysConfigService configService;
    private final CaptchaCore captchaCore;
    @GetMapping("/captchaImage")
    public Map<String,Object> getCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultJson json = ResultJson.success();
        boolean captchaEnabled = configService.selectCaptchaEnabled();
        json.add("captchaEnabled", captchaEnabled);
        if (!captchaEnabled)
        {
            return json;
        }
        return captchaCore.createCode(request, response,json);
    }
}
